/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.security;

import com.thienloc.omap.controller.ApplicationBean;
import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.OmapRole;
import com.thienloc.omap.entity.OmapUser;
import com.thienloc.omap.entity.Permission;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class ActiveUserBean extends BaseBean implements Serializable {

    private final Logger logger = LoggerFactory.getLogger(ActiveUserBean.class);
    private List<OmapUser> list;
    @ManagedProperty(value = "#{applicationBean}")
    private ApplicationBean applicationBean;
    private OmapUser selected;
    private OmapUser[] selectedList;

    @PostConstruct
    public void init() {
        logger.trace("Getting Logged in users in the application bean");
        try {
            list = new ArrayList<OmapUser>();
            list.addAll(applicationBean.getLoggedInUsers().values());
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
    }

    public void removeSelected() {
        try {
            int size = selectedList.length;
            if (size == 0) {
                JsfUtil.addMessage(FacesMessage.SEVERITY_WARN, "Không có gì để xóa", "Hãy click chọn các bản ghi cần xóa");
                return;
            }

            for (OmapUser item : selectedList) {
                logger.info("[{}]: Removing {}({}) from the list of logged in users in application bean", new Object[] {getUsername(), item.getUsername(), item.getUserId()});
                applicationBean.removeLoggedInUser(item.getUsername());
            }

            JsfUtil.addSuccessMessage("Thành công!", "Đã bỏ session của " + size + " người dùng");
            init();
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
    }
    
    public List<String> getAllEffectivePermissions(OmapUser omapUser){
        List<String> result = new ArrayList<String>();
        List<Permission> baseUserPermissions = omapUser.getPermissionList();
        for (Permission permission : baseUserPermissions) {
            result.add(permission.getResourceId().getName() + " - " + permission.getAction());
        }
        
        List<OmapRole> roles = omapUser.getOmapRoleList();
        for (OmapRole omapRole : roles) {
            List<Permission> rolePermissions = omapRole.getPermissionList();
            for (Permission permission : rolePermissions) {
                String entry = permission.getResourceId().getName() + " - " + permission.getAction();
                if (!result.contains(entry)) {
                    result.add(entry);
                }
            }
        }
        
        return result;
    }

    protected void logAndNotifyError(Exception ex) {
        String error = "Lỗi! ";
        logger.error("[{}]: ", getUsername(), ex);
        JsfUtil.addErrorMessage(error, ex.getMessage());
    }

    public OmapUser[] getSelectedList() {
        return selectedList;
    }

    public void setSelectedList(OmapUser[] selectedList) {
        this.selectedList = selectedList;
    }

    public OmapUser getSelected() {
        return selected;
    }

    public void setSelected(OmapUser selected) {
        this.selected = selected;
    }

    public ApplicationBean getApplicationBean() {
        return applicationBean;
    }

    public void setApplicationBean(ApplicationBean applicationBean) {
        this.applicationBean = applicationBean;
    }

    public List<OmapUser> getList() {
        return list;
    }

    public void setList(List<OmapUser> list) {
        this.list = list;
    }
}
